<?php
/**
 * Ecga首页
 *
 * PHP version 5
 *
 * @package   Ecga
 * @author    Garbin <huangjiabin@ec3s.com>
 * @copyright 2010 Xingchangxinda Inc.
 * @version   SVN: $Id$
 */
namespace Project\Apps\Ecga\Controllers;

use \Project\Apps\Ecga\Controllers\Abstraction\EcgaController;
use \Project\Apps\Ecga\Library\Visitor;
use \Framework\System\Helper\HttpResponse;
use \Framework\System\Abstractions\PassportException;

/**
 * Ecga首页
 *
 * PHP version 5
 *
 * @package   Ecga
 * @author    Garbin <huangjiabin@ec3s.com>
 * @copyright 2010 Xingchangxinda Inc.
 * @version   Release: <0.1>
 **/
class Profile extends EcgaController
{
    /**
     * 登录页不需要验证
     *
     * @param string $act 动作
     *
     * @author Garbin
     * @return void
     */
    public static function validate($act)
    {
        return true;
    }

    /**
     * login
     *
     * @param HttpRequest $request 请求对象
     *
     * @return void
     */
    public static function login($request)
    {
        $page = self::createPage();
        HttpResponse::output($page->render('login'));
    }

    /**
     * 登录处理方法
     *
     * @param request $request 请求对象
     *
     * @author Garbin
     * @return void
     */
    public static function doLogin($request)
    {
        $email    = $request->post('email', 'string');
        $password = $request->post('password', 'string');
        $remember = $request->post('remember', 'bool');
        if ($email == '') {
            self::message('对不起，您没有填写邮件地址！', 'warning', './profile.php?a=login');
            return;
        }

        if (empty($password)) {
            self::message('对不起，请输入密码！', 'warning', './profile.php?a=login');
            return;
        }

        try {
            $user = Visitor::getInstance();
			$user->getPassport()->login($email, $password);
			if ($remember == true) {
				$user->getPassport()->setRemember();
			}
            HttpResponse::redirect('./index.php');
        } catch (PassportException $ex) {
            self::message($ex->getMessage(), 'warning');
            self::login($request);
		}
    }

    /**
     * logout
     *
     * @param HttpRequest $request 请求对象
     *
     * @return void
     */
    public static function logout($request)
    {
        $user = Visitor::getInstance();
        $user->getPassport()->logout();
		$user->getPassport()->outRemember();
        self::login($request);
    }

    /**
     * 管理员修改密码
     *
     * @param HttpRequest $request http请求对象
     *
     * @return void
     * @author Confu
     */
    public static function modifyPassword($request)
    {
         $page = self::createPage();
         HttpResponse::output($page->render('modifypassword'));
    }


    /**
     * 管理员确认修改密码
     *
     * @param HttpRequest $request http请求对象
     *
     * @return void
     * @author Confu
     */
    public static function doModifyPassword($request)
    {
        /*验证原密码*/
        $originPassword = $request->post('originPassword', 'string');
        if (empty($originPassword)) {
            HttpResponse::jsonResult(false, '对不起，原密码不能为空，请输入');
            return;
        }

        try {
            Visitor::getInstance()->getPassport()->motifyPassword(
                $originPassword,
                $request->post('newPassword', 'string'),
                $request->post('repeatNewPassword', 'string')
            );
        } catch (PassportException $ex) {
            HttpResponse::jsonResult(false, $ex->getMessage());
        }
        
        HttpResponse::jsonResult(true, '密码修改成功');
    }

    // public static function test($request)
    // {
    //     \Project\Apps\Ecga\Jobs\ProductRelevance::_saveRelevanceDate('2012-01-02');
    // }
}

